package com.example.onlineeducation.viewmodel


import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.onlineeducation.repository.UserRepository
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.json.LoginJson
import com.example.onlineeducation.data.model.CertificateListDataModel
import com.example.onlineeducation.data.model.RegisterSearchGroupListDataModel
import com.example.onlineeducation.data.model.RegisterSearchProjectListDataModel
import com.example.onlineeducation.data.model.RegisterSearchWorkTypeListDataModel
import com.example.onlineeducation.repository.SearchRepository
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest


/**
 * Creator: yc
 * Date: 2021/6/15 18:03
 * UseDes:
 */
class RegisterVM : BaseViewModel() {

    private val mSearchRepository: SearchRepository by lazy { SearchRepository(mApiService) }
    private val mUserRepository: UserRepository by lazy { UserRepository(mApiService) }

    private val _mProList = MutableLiveData<PagingData<RegisterSearchProjectListDataModel>>()
    val mProList: LiveData<PagingData<RegisterSearchProjectListDataModel>> = _mProList

    fun getProList(proName: String) = ycLaunch {
        mSearchRepository.getProList(proName).cachedIn(viewModelScope).collectLatest {
            _mProList.postValue(it)
        }
    }

    private val _mGroupList = MutableLiveData<PagingData<RegisterSearchGroupListDataModel>>()
    val mGroupList: LiveData<PagingData<RegisterSearchGroupListDataModel>> = _mGroupList

    fun getGroupList(proName: String,proId: String) = ycLaunch {
        mSearchRepository.getGroupList(proId,proName).cachedIn(viewModelScope).collectLatest {
            _mGroupList.postValue(it)
        }
    }

    private val _mWorkTypeList = MutableLiveData<PagingData<RegisterSearchWorkTypeListDataModel>>()
    val mWorkTypeList: LiveData<PagingData<RegisterSearchWorkTypeListDataModel>> = _mWorkTypeList

    fun getWorkTypeList(workTypeName: String) = ycLaunch {
        mSearchRepository.getWorkTypeList(workTypeName).cachedIn(viewModelScope).collectLatest {
            _mWorkTypeList.postValue(it)
        }
    }

    private val _mRegister = MutableLiveData<YcResult<String>>()
    val mRegister: LiveData<YcResult<String>> = _mRegister

    fun register(name: String, phone: String,idCard: String, sex: String
              ,projId: String, groupId: String,personTypeId: String, dutyName: String) = ycLaunch {
        mUserRepository.register(name, phone,idCard,sex,projId,groupId,personTypeId,dutyName).ycHasLoading().collectLatest { result ->
            _mRegister.postValue(result)
        }
    }
}


